import axios from "axios";
import { ElMessage } from "element-plus";
export function request(config) {
    const instance = axios.create({
        baseURL: 'https://recruit.aicase.xyz',
        timeout: 5000
    });
    //请求拦截器
    instance.interceptors.request.use(config => {
        // 在发送请求之前做些什么
        const token = localStorage.getItem('Authorization')
        //不需要header参数的
        const withUrl = ['/api/boss/login']
        //进行判断
        if (token && !withUrl.includes(config.url)) {
            config.headers['Authorization'] = token
        }
        return config;
    }, function (error) {
        // 对请求错误做些什么
        return Promise.reject(error);
    });

    //响应拦截器
    instance.interceptors.response.use(response => {
        // 对接口异常的数据，给用户提示
        if (response.data.code === 500) {
            ElMessage.warning(response.data.message)
        }
        //token过期失效或错误清除token
        if (response.data.code === 401) {
            localStorage.removeItem('Authorization')
            window.location.href = window.location.origin
        }
        // console.log(response);
        return response;
    }, error => {
        return Promise.reject(error);
    });
    return instance(config);
}
